package com.k.frame.boot.boot.service.controller;

import com.google.common.collect.Maps;
import com.k.frame.boot.boot.service.feign.OrderClient;
import com.k.frame.boot.boot.service.feign.OrderDetailClient;
import com.k.frame.boot.boot.service.feign.ProductDetailClient;
import com.k.frame.boot.boot.service.model.ServiceModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Slf4j
@RestController
public class ServiceControl {
    @Value("${serviceName:v1}")
    public String serviceName;
    @Value("${serviceDelay:0}")
    private int serviceDelay;
    @Autowired
    private OrderClient orderClient;
    @Autowired
    private OrderDetailClient orderDetailClient;
    @Autowired
    private ProductDetailClient productDetailClient;

    @RequestMapping("/index")
    public ServiceModel index() {
        log.info("index_{}", serviceName);
        ServiceModel model = new ServiceModel();
        model.setServiceName(serviceName);
        return model;
    }

    @RequestMapping("/version")
    public ServiceModel version() {
        log.info("version_{}", serviceName);
        ServiceModel model = new ServiceModel();
        model.setServiceName(serviceName);
        return model;
    }

    /**
     * prod->order->orderdetail
     * ->productdetail
     */
    @RequestMapping("/product")
    public ServiceModel product() {
        log.info("productDetail_{}", serviceName);
        ServiceModel productDetail = productDetailClient.productDetail();
        ServiceModel order = orderClient.order();

        ServiceModel model = new ServiceModel();
        model.setServiceName(serviceName);
        model.setOrder(order);
        model.setProductDetail(productDetail);
        return model;
    }
    @RequestMapping("/productWithDetail")
    public ServiceModel productWithDetail() {
        log.info("productWithDetail_{}", serviceName);
        ServiceModel productDetail = productDetailClient.productDetail();
        ServiceModel model = new ServiceModel();
        model.setServiceName(serviceName);
        model.setProductDetail(productDetail);
        return model;
    }
    @RequestMapping("/order")
    public ServiceModel order() {
        log.info("detail_{}", serviceName);
        ServiceModel model = new ServiceModel();
        model.setServiceName(serviceName);
        ServiceModel orderDetail = orderDetailClient.orderDetail();
        model.setOrderDetail(orderDetail);
        return model;
    }

    @RequestMapping("/orderDetail")
    public ServiceModel orderDetail() {
        log.info("orderDetail_{}", serviceName);
        ServiceModel model = new ServiceModel();
        model.setServiceName(serviceName);
        return model;
    }

    @RequestMapping("/productDetail")
    public ServiceModel productDetail() {
        log.info("productDetail_{}", serviceName);
        ServiceModel model = new ServiceModel();
        model.setServiceName(serviceName);
        return model;
    }

    @RequestMapping("/delay")
    public Map<String, String> delay() throws InterruptedException {
        log.info("delay_{}", serviceName);
        Thread.sleep(serviceDelay);
        Map<String, String> map = Maps.newHashMap();
        map.put("version", serviceName);
        return map;
    }
}
